package com.danco.training.zaneuskaya.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.domain.Education;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.CityDao;
import com.danco.training.zaneuskaya.repository.CountryDao;
import com.danco.training.zaneuskaya.repository.EducationDao;
import com.danco.training.zaneuskaya.repository.UserDao;
import com.danco.training.zaneuskaya.service.EducationService;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("educationService")
@Transactional
public class EducationServiceImpl implements EducationService {

	private static final String EDUCATION_IS_NULL = "Education is null";

	private static final Logger LOGGER = Logger
			.getLogger(EducationServiceImpl.class);

	@Autowired
	private EducationDao educationDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private CityDao cityDao;

	@Autowired
	private CountryDao countryDao;

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public CountryDao getCountryDao() {
		return countryDao;
	}

	public void setCountryDao(CountryDao countryDao) {
		this.countryDao = countryDao;
	}

	public EducationDao getEducationDao() {
		return educationDao;
	}

	public void setEducationDao(EducationDao educationDao) {
		this.educationDao = educationDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public void createEntity(Education t) {
		try {

			educationDao.create(t);

		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}

	}

	@Override
	public void deleteEntityById(Integer id) {
		try {
			Education education = educationDao.read(id);
			educationDao.delete(education);
		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}

	}

	@Override
	public void updateEntity(Education t) {
		try {
			Education education = educationDao.read(t.getEducationId());
			education.setEducationCity(t.getEducationCity());
			education.setEducationCountry(t.getEducationCountry());
			education.setEducationEnd(t.getEducationEnd());
			education.setEducationStart(t.getEducationStart());
			education.setInstitutionTitle(t.getInstitutionTitle());
			educationDao.update(education);

		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}

	}

	@Override
	public Education getEntityById(Integer id) {
		Education education = null;
		try {
			education = educationDao.read(id);
			if (education == null) {
				throw new NullPointerException(EDUCATION_IS_NULL);
			}
		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return education;
	}

	@Override
	public List<Education> getEntityList() {
		List<Education> educations = null;
		try {
			educations = educationDao.readAll();
		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return educations;
	}

	@Override
	public List<Education> getEducationListByLocation(City city, Country country) {
		List<Education> educations = null;
		try {
			city = cityDao.read(city.getCityId());
			country = countryDao.read(country.getCountryId());
			if (city == null) {
				educations = educationDao.findEducationsByCountry(country);
			} else {
				educations = educationDao.findEducationsByLocation(country,
						city);
			}
		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return educations;
	}

	@Override
	public List<Education> getUserEducationList(User user) {
		List<Education> educations = null;
		try {
			user = userDao.read(user.getUserId());

			educations = educationDao.findEducationByUser(user);

		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return educations;
	}

	@Override
	public List<Education> getUserEducationListByType(User user, String type) {
		List<Education> educations = null;
		try {
			user = userDao.read(user.getUserId());
			educations = educationDao.findUserEducationsByType(type, user);

		} catch (NullPointerException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return educations;
	}

}
